package net.oschina.app.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import net.oschina.app.R;
import net.oschina.app.constant.Constants;
import net.oschina.app.util.PreferencesUtils;
import net.oschina.app.util.UIUtils;

/**
 * Created by YL on 2017/4/5.
 */
public class HostActivity extends Activity {
    private android.widget.EditText hostname;
    private android.widget.EditText hostpost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.host_activity);
        this.hostpost = (EditText) findViewById(R.id.host_post);
        this.hostname = (EditText) findViewById(R.id.host_name);
        hostname.setText(Constants.Name);
        hostpost.setText(Constants.Post);
    }

    public void save(View v) {
        String post = hostpost.getText().toString().trim();
        String name = hostname.getText().toString().trim();
        Constants.Name = name;
        Constants.Post = post;
        saveToSP(post, name);
        Constants.refreshUrl();
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }

    private void saveToSP(String post, String name) {
        PreferencesUtils.putString(UIUtils.getContext(),"name",name);
        PreferencesUtils.putString(UIUtils.getContext(),"post",post);
    }

    public void clear(View v) {
        hostpost.setText("");
        hostname.setText("");
        String name = "10.0.2.2";
        String post = "8080";

        Constants.Name = name;
        Constants.Post = post;
        saveToSP(post, name);
        Constants.refreshUrl();
        Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();

    }
}
